package cn.tedu.charging.order;


import cn.tedu.charging.order.pojo.po.RabbitmqDeadPo;
import cn.tedu.charging.order.rabbitmq.RabbitMQProducter;
import com.rabbitmq.client.*;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@SpringBootTest
public class RabittmqService {

    @Autowired
    private RabbitMQProducter rabbitMQProducter;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendMsgPlus(){
        RabbitmqDeadPo rabbitmqDeadPo = new RabbitmqDeadPo();
        rabbitmqDeadPo.setOrderNo("1234567899999");
        rabbitMQProducter.sendMsg(rabbitmqDeadPo);
    }

    @Test
    public  void sendMsgdd(){
        rabbitTemplate.convertAndSend("test-add-exchange-1","t-a-q-5111","hhh");
    }


    @Test
    public void sendMsg(){
        //原生方式创建信道 发送消息
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        try {
            Connection connection = connectionFactory.newConnection();
            String exchange="test-add-exchange-1";
            String routerKey="t-a-q-4";
            String queue="test-add-queue-4";
            AMQP.BasicProperties properties = null;
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(exchange, BuiltinExchangeType.FANOUT);
            channel.queueDeclare(queue,true,false,false,null);
            channel.queueBind(queue,exchange,routerKey);
//            channel.basicPublish(exchange,routerKey,properties,"hhhhh".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }

    }
}
